import request from '@/utils/request';

// 定义用户相关接口的类型
export interface User {
  user_id?: number;
  account: string;
  password?: string;
  name: string;
  signature?: string;
  avatar?: string;
  avatar_upload_id?: number | null; // 新增字段，用于关联上传的头像文件
  role?: string;
}

export interface LoginParams {
  account: string;
  password: string;
}

export interface LoginResult {
  user: Omit<User, 'password'>;
  token: string;
}

// 用户登录
export const login = (data: LoginParams) => {
  return request.post<LoginResult>('/user/login', data);
};

// 用户注销
export const logout = () => {
  return request.post('/user/logout');
};

// 获取用户列表
export const getUserList = (data?: { keyword?: string; sort_field?: string; sort_order?: string }) => {
  return request.post<User[]>('/user/getUser', data);
};

// 获取用户列表（分页）
export const getUserListPage = (data: { page?: number; limit?: number; keyword?: string; sort_field?: string; sort_order?: string }) => {
  return request.post<{ list: User[]; pagination: any }>('/user/getUserList', data);
};

// 根据ID获取用户信息
export const getUserById = (user_id: number) => {
  return request.post<User>(`/user/getUserById`, { user_id });
};

// 新增用户
export const addUser = (data: Omit<User, 'user_id'>) => {
  return request.post('/user/addUser', data);
};

// 编辑用户信息
export const updateUser = (data: Partial<User> & { user_id: number }) => {
  return request.post('/user/updateUser', data);
};

// 删除用户
export const deleteUser = (user_id: number) => {
  return request.post('/user/deleteUser', { user_id });
};

// 批量删除用户
export const batchDeleteUser = (user_ids: number[]) => {
  return request.post('/user/batchDeleteUser', { user_ids });
};